<template>
    <!-- 
    <b-modal ref="my-modal" hide-footer title="Using Component Methods">
     <div class="d-block text-center">
       <h3>Hello From My Modal!</h3>
     </div>
     <b-button class="mt-3" variant="outline-danger" block @click="hideModal">Close Me</b-button>
     <b-button class="mt-2" variant="outline-warning" block @click="toggleModal">Toggle Me</b-button>
   </b-modal> 
    -->
    <!--
        body-class="modalStyle" 表示样式
        size="lg" 表示模态窗口大小 xl,lg,sm,full
        hide-footer 表示隐藏底部按钮
        hide-header 表示隐藏头部内容
        no-close-on-backdrop 表示鼠标点击背景不可关闭
        hide-header-close 表示隐藏头部关闭按钮
        centered 居中
        ok-title=“确定” 
        cancel-title=取消
        @ok="handleOk" 
        @cancel="handleCancel"
        scrollable 滚动条
        hide-backdrop 隐藏背景
        wrapClassName="ant-modal-cust-warp" 
        style="top:5%;height: 85%;overflow-y: hidden" 样式
        :modal-class="my-modal"
        dialog-class="modal-dialog"
        @hidden 关闭窗体触发事件
    -->
    <b-modal ref="my-modal" :title="title" scrollable @hidden="onHidden" size="xl" hide-footer no-close-on-backdrop>
        <div class="d-block text-center">
            <div class="form-group m-form__group row">
                <label class="col-lg-1 col-form-label">类型</label>
                <div class="col-lg-2">
                    <el-select v-model="sourcesID" placeholder="请选择">
                        <el-option value="">请选择</el-option>
                        <el-option
                            v-for="item in [{value:'0',label:'bpmn'},{value:'1',label:'graph'},{value:'2',label:'img'}]"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </div>
                <button type="button" class="btn btn-light-dark mr-2 m-btn m-btn--custom m-btn--icon"  @click="formatCode">
                    <span><i class="fas fa-remove-format"></i><span>格式化</span></span>
                </button>
                <button type="button" class="btn btn-light-dark mr-2 m-btn m-btn--custom m-btn--icon" @click="copyLcReSources();">
                    <span><i class="fas fa-copy"></i><span>复制</span></span>
                </button>
            </div>
            <div class="form-group m-form__group row">
                <div class="col-lg-12">
                    <!-- 
                    <Codemirror
                    :value="sqlContent.sqltext"
                    language="sql"
                    @input="changeTextarea"
                    style="height: 60vh"
                    >
                    </Codemirror>
                     -->
                    <Codemirror
                    :value="xmlContent"
                    language="xml"   
                    ref="CodemirrorRef"
                    @input="changeTextarea"    
                    style="height: 60vh"
                    ></Codemirror>
                    
                </div>
            </div>
        </div>
        <template slot="modal-footer">
            <!--自定义按钮-->
        </template>
    </b-modal>
</template>
<script type="text/javascript">
import Codemirror from '@/components/codemirror.vue'
import apiUtil from "@/core/util/apiUtil.js";
import { handleNotify, handleAlert, handleConfirm, showWating, closeWating } from "@/core/util/jehcUtil.js";
export default {
    data() {
        return {            
            title: "源码",
            sourcesID: "",
            id: "",
            currentBPMN: "",
            currentImg: "",
            currentMxgraph: "",
            xmlContent: "",//xml方式
            sqlContent: {
                sqltext: '' //sql方式
            }
        }
    },
    components: { 
        Codemirror,
    },
    watch: {
        "sourcesID": {//监听字段变化
            handler: function (newVal, old) {
                if (newVal == "" || newVal == undefined || newVal == null) {
                    this.xmlContent = ""
                    this.setCodeContent(this.xmlContent)
                } else {
                    if (newVal == 0) {
                        this.xmlContent = this.currentBPMN
                    }
                    if (newVal == 1) {
                        this.xmlContent = this.currentMxgraph

                    }
                    if (newVal == 2) {
                        this.xmlContent = this.currentImg
                    }                    
                    this.setCodeContent(this.xmlContent)
                }
            }
        },
    },
    mounted() {
       
    },
    methods: {
        showModal(row) {
            this.$refs['my-modal'].show();
            if (null != row && undefined != row) {
                this.id = row.id;
                this.title = "源码【" + row.title + "】";
                this.initCodeWin(row);
            }

        },
        hideModal() {
            this.$refs['my-modal'].hide()
        },
        toggleModal() {
            // 当模态已隐藏时，我们传递要返回焦点的按钮的ID
            this.$refs['my-modal'].toggle('#toggle-btn')
        },
        onHidden() {//关闭窗体触发事件
            this.$emit("change", null);
        },
        
        initCodeWin(row) {        
            apiUtil.get(process.env.VUE_APP_BPM_API + "/lcProcess/get/" + row.id).then(({ data }) => {
                this.currentBPMN = data.data.bpmn;
                this.currentImg = data.data.imgXml;
                this.currentMxgraph = data.data.mxgraphXml;
            });
        },
        changeTextarea(val) {
            this.xmlContent = val
        },
        formatCode(){
            this.$refs.CodemirrorRef.formatCode();
        },
        setCodeContent(value){
            this.$refs.CodemirrorRef.setCodeContent(value);
        },
        copyLcReSources(){
            this.$refs.CodemirrorRef.copyCode();
        }
    }
}
</script>
